package com.temi.demo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.temi.demo.R;
import com.temi.demo.bean.ContactsListResult;
import com.temi.demo.utils.TimeUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by coco on 2018/6/26.
 */

public class ContactsListAdapter extends BaseAdapter<ContactsListResult.ContactsData, ContactsListAdapter.ContactsViewHolder> {

    private ItemClickListener mItemClickListener;

    public ContactsListAdapter(Context context, List<ContactsListResult.ContactsData> dataList) {
        super(context, dataList);
    }

    @Override
    public int getLayout() {
        return R.layout.item_recyclerview_contacts;
    }

    @Override
    public ContactsViewHolder createVHolder(ViewGroup parent, int viewType, View view) {
        return new ContactsViewHolder(view);
    }

    @Override
    public void bindViews(ContactsViewHolder holder, int position) {
        ContactsListResult.ContactsData bean = mDataList.get(position);

        Glide.with(mContext)
                .load(bean.getAvatar())
                .apply(RequestOptions.circleCropTransform()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(holder.img);

        String wholeName = bean.getFirst_name() + " " + bean.getLast_name();
        holder.name.setText(wholeName);

        String time = bean.getDate();
        holder.time.setText(TimeUtil.getTimeFormatText(time));

        holder.bindPosition(position);

    }

    class ContactsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.user_icon)
        protected ImageView img;

        @BindView(R.id.item_recyclerView_contacts_name)
        protected TextView name;

        @BindView(R.id.item_recyclerView_contacts_time)
        protected TextView time;

        private int position;

        ContactsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        public void bindPosition(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, position);
            }
        }
    }

    public void setClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}
